package com.gzgy.ustore.api.common.util;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by jancekyang on 2017/7/14.
 */
public class ParamUtil {

    /**
     * 键值对 放进 map 集合
     */
    public static Map<String, String> paramToMap(String param) {
        Map<String, String> mapRequest = new HashMap<String, String>();

        String[] arrSplit = null;

        String strUrlParam = param.trim();
        if (strUrlParam == null) {
            return mapRequest;
        }
        //每个键值为一组
        arrSplit = strUrlParam.split("[&]");
        for (String strSplit : arrSplit) {
            String[] arrSplitEqual = null;
            arrSplitEqual = strSplit.split("[=]");

            //解析出键值
            if (arrSplitEqual.length > 1) {
                //正确解析
                mapRequest.put(arrSplitEqual[0], arrSplitEqual[1]);

            } else {
                if (arrSplitEqual[0] != "") {
                    //只有参数没有值，不加入
                    mapRequest.put(arrSplitEqual[0], "");
                }
            }
        }
        return mapRequest;
    }

    public static void main(String[] args) {

       String param = "companyId=123&userId=1&orgTreeId=1&dvcGroupId=2&status=1&       ";
        Map<String, String> map = paramToMap(param);
        for (String key : map.keySet()) {
            System.out.println("key= " + key + " and value= " + map.get(key));
        }
        System.out.println(map.get("asd")==null);
        //System.out.println(map.get("companyId"));
    }

}
